﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class Departments : System.Web.UI.Page
    {
        DepartmentDAO dao = new DepartmentDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                lvDepartments.DataSource = dao.GetAll(((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                lvDepartments.DataBind();
                btnEditSuccess.Visible = false;
            }
            catch (Exception ex)
            {   
                Common.logs( "Groups-Load", ex, Common.logType.error);
            }
        }

        protected void btnCreate_Click(object sender, CommandEventArgs e)
        {
            DepartmentDTO dto = new DepartmentDTO();
            dto.DepartmentName = txtDepartmentName.Text;
            dto.Description = txtDescription.Text;

            if (String.IsNullOrWhiteSpace(dto.DepartmentName))
            {
                lbNotice.Text = "Tên phòng ban không được để trống.";
            }
            else
            {
                string resultCheckName = dao.CheckName(dto.DepartmentName, 0,((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);

                if (!String.IsNullOrWhiteSpace(resultCheckName))
                {
                    lbNotice.Text = resultCheckName;
                }
                else
                {
                    try
                    {
                        dto.UserID = ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID;
                        dao.Add(dto);
                        Response.Redirect(Request.RawUrl);
                    }
                    catch (Exception ex)
                    {
                        Common.logs("Groups-Create", ex, Common.logType.error);
                    }
                    
                }
            }
        }

        protected void btnDel_Click(object sender, CommandEventArgs e)
        {
            try
            {
                dao.Delete(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs( "Groups-Del", ex, Common.logType.error);
            }
            
        }

        protected void btnEdit_Click(object sender, CommandEventArgs e)
        {
            DepartmentDTO dto = dao.GetByDepartmentID(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
            txtDepartmentName.Text = dto.DepartmentName;
            txtDescription.Text = dto.Description;
            btnCreate.Visible = false;
            btnEditSuccess.Visible = true;
            btnEditSuccess.CommandArgument = e.CommandArgument.ToString();
        }

        protected void btnEditSuccess_Click(object sender, CommandEventArgs e)
        {
            DepartmentDTO dto = dao.GetByDepartmentID(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
            dto.DepartmentName = txtDepartmentName.Text;
            dto.Description = txtDescription.Text;

            if (String.IsNullOrWhiteSpace(dto.DepartmentName))
            {
                lbNotice.Text = "Tên phòng ban không được để trống.";
            }
            else
            {
                string resultCheckName = dao.CheckName(dto.DepartmentName, Convert.ToInt32(e.CommandArgument),((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);

                if (!String.IsNullOrWhiteSpace(resultCheckName))
                {
                    lbNotice.Text = resultCheckName;
                }
                else
                {
                    try
                    {
                        dao.Edit(dto);
                        Response.Redirect(Request.RawUrl);
                    }
                    catch (Exception ex)
                    {
                        Common.logs("Groups-Edit", ex, Common.logType.error);
                    }
                }

                btnEditSuccess.Visible = true;
            }
        }
    }
}